package com.salton123.ui.base

import android.app.Activity
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import com.hyphenate.easeim.section.base.BaseInitFragment
import com.youme.viewmodel.YouMeApiViewModel

/**
 * @Time:2021/4/19 15:58
 * @Author:wujinsheng
 * @Description:
 */
abstract class YoumeBaseFragment : BaseInitFragment(), View.OnClickListener {
    lateinit var mViewModel: YouMeApiViewModel

    abstract fun initViewAndData()
    override fun initData() {
        super.initData()
        mViewModel = ViewModelProvider(this).get(YouMeApiViewModel::class.java)
        initViewAndData()
    }

    open fun setListener(vararg views: View) {
        for (view in views) {
            view.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
    }

    open fun shortToast(toast: String?) {
        if (activity() != null) {
            Toast.makeText(activity(), toast, Toast.LENGTH_SHORT).show()
        }
    }

    private lateinit var mHostActivity: FragmentActivity
    fun activity(): FragmentActivity {
        mHostActivity = when (this) {
            is Fragment -> {
                this.activity!!
            }
            is Activity -> {
                this as FragmentActivity
            }
            else -> {
                throw RuntimeException("instance must Fragment or Activity")
            }
        }
        return mHostActivity
    }
}